package edu.njfu.myapplication

fun main(){
    println("hello world!");
    val a:Int=3;
    var b:Long=5;
    val c=0;
    b+=a;
    println(a)
    println(b)
    println(add(a,b))
    printRectangel(10)
    print( hello("张三"))
    print2("李四")
    //member function
    var rt=Rectangle
    rt.printArea(23,45)
    printArea(23,45)
    fizzbuzz4(0,50)
    //foo(9)


    //命名参数,程序的可读性更高！
    val string = "a kindness of ravens"
    string.regionMatches(14, "Red Ravens", 4, 6, true)
    string.regionMatches(thisOffset = 14, other = "Red Ravens",
        otherOffset = 4, length = 6, ignoreCase = true)

   //if
    println(largerNumber(3,4))
    println(largerNumber(5,4))
    //when
    println(getScore("Tom"))
    println(getScore("Lily"))
    //range
    println(sum100())
    //until
    println(sumutil())
    //object
    //创建对象不需要new关键字
    val p1=Person()
    p1.name="张三"
    p1.age=18
    p1.eat()

    //类型之间的转换
    /**
     * 8种基本类型
     *  Int  int
     *  Long  long
     *  Short short
     *  Byte  byte
     *  ----------
     *  Float float
     *  Double double
     *  ----------
     *  Boolean boolean
     *  ---------
     *  Char   char
     *  ---------
     *  有各种转换方法可以使用.toXXX()
     */
    var int1:Int=9;
    //cast
    var long1:Long=int1.toLong();

}

fun add(num1:Int,num2:Long):Long{
    return num1+num2;
}
//top-level function，全局
//member function
//local function,   局部嵌套函数
//扩展函数
//闭包，
//高阶函数
fun printRectangel(n:Int){
    for(  i in 1..n){
        for(  j in 1..n){
            print(" #")
        }
        println("")
    }
}
//如果函数只有一条,可以省略大括号

fun  hello(name:String):String = "hello "+name;

//fun hello():String{
//    return "hello world!";
//}
//函数没有返回值，使用Unit，相当于java中的void
fun print2(str:String):Unit=println(str)
//member function
//snipaste
object Rectangle {
    fun printArea(width: Int, height: Int): Unit {
        val area = calculateArea(width, height)
        println("The area is $area")
    }
    fun calculateArea(width: Int, height: Int): Int {
        return width * height
    }
}
//local function,在函数内部可以定义函数，嵌套定义
fun printArea(width:Int,height:Int):Unit{
    fun calculateArea(width:Int,height:Int):Int=width*height
    var area=calculateArea(width,height)
    println("the area is $area")
}
//嵌套定义的优点
fun fizzbuzz(start: Int, end: Int): Unit {
    for (k in start..end) {
        if (k % 3 == 0 && k % 5 == 0)
            println("Fizz Buzz")
        else if (k % 3 == 0)
            println("Fizz")
        else if (k % 5 == 0)
            println("Buzz")
        else
            println(k)
    }
}
fun fizzbuzz2(start: Int, end: Int): Unit {
    fun isFizz(k: Int): Boolean = k % 3 == 0
    fun isBuzz(k: Int): Boolean = k % 5 == 0
    for (k in start..end) {
        if (isFizz(k) && isBuzz(k))
            println("Fizz Buzz")
        else if (isFizz(k))
            println("Fizz")
        else if (isBuzz(k))
            println("Buzz")
        else
            println(k)
    }
}
fun fizzbuzz4(start: Int, end: Int): Unit {
    for (k in start..end) {
        fun isFizz(): Boolean = k % 3 == 0
        fun isBuzz(): Boolean = k % 5 == 0
        //相当于java中的switch ...case
        when {
            isFizz() && isBuzz() -> println("Fizz Buzz")
            isFizz() -> println("Fizz")
            isBuzz() -> println("Buzz")
            else -> println(k)
        }
    }
}

fun foo(k:Int):Unit{
    require(k>10,{"K should be greater than 10"});
    print("the k'value is $k")
}
fun foo2(a:Int):Unit{
    check(a<5,{"a must be less than 5"});
    print(a);
}
//默认参数
class Student(val name: String, val registered: Boolean, credits:  Int){
    constructor(name: String) : this(name, false, 0)
    constructor(name: String, registered: Boolean) : this(name,registered, 0)
}
//默认值
class Student2(val name: String, val registered: Boolean = false, credits: Int = 0)

//if
//if statement
fun largerNumber(num1: Int, num2: Int) = if (num1 > num2) {
    num1
} else {
    num2
}
fun largerNumber2(num1: Int, num2: Int) = if (num1 > num2) num1 else num2
//when
fun getScore(name: String) = when (name) {
    "Tom" -> 86
    "Jim" -> 77
    "Jack" -> 95
    "Lily" -> 100
    else -> 0
}
//range  .. 左右都是闭区间，其他语言都是左闭右开！
fun sum100():Int{
    var sum=0
    for(i in 1..100){
        sum+=i
    }
    return sum;
}
//until,左闭右开
fun sumutil():Int{
    var sum=0
    for(i in 1 until 100){
        sum+=i
    }
    return sum;
}

//定义对象
class Person {
    var name = ""
    var age = 0

    fun eat() {
        println(name + " is eating. He is " + age + " years old.")
    }
}
//interface
interface Study {
    fun readBooks()
    fun doHomework()
}

class Student3(val name: String, val age: Int) : Study {
    override fun readBooks() {
        TODO("Not yet implemented")
        println(name + " is reading.")

    }

    override fun doHomework() {
        TODO("Not yet implemented")
        println(name + " is reading.")
    }

}
